window.onload = function() {
    goods_setTransferTable();
}
function goods_setTransferTable() {
    setTransferTable(
            Array("goodsList"),
            Array("Bdragtable"),
            "li",
            goods_createLiNode,
            Array("hidSupplierNo", "hidSupplierName")
    );
    sortable("Bdragtable", "li");
    setSortableStopEvent("Bdragtable",goods_order);
}
function goods_order(){
    li_order("Bdragtable");
}

function goods_up(strLiId){
    li_up(strLiId, "Bdragtable")
}

function goods_down(strLiId){
    li_down(strLiId, "Bdragtable")
}

function goods_deleteGoods(strLiId) {
    li_del(strLiId, "Bdragtable", "goodsList");
    goods_order();
}

function goods_createLiNode(arrValues) {
    var strLi;
    var i = 1;
    objEach("span[id='liNo']", function(k){
        i = k + 2
    });
    i = parseInt(i);
    strLi = " <li id=\"Bdragtable"+ arrValues[0]+ "\" class=\"ui-state-default\" >"+
                 "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"BNoMarginTable\">" +
                     "<tr>" +
                         "<td width=\"20%\" align=\"right\" >" + 
                             "<span id=\"liNo\">" + i + "</span>" + 
                             "<input type = \"hidden\" id = \"supplierNo\" value = \"" + arrValues[0] + "\" />" +
                         "</td>" +
                         "<td width=\"20%\"><div class=\"Ctxtparagraph\" >" + arrValues[1] + "<div></td>" +
                         "<td width=\"30%\" align=\"right\" >" + 
                         "<input type=\"text\" id=\"txtNum\"  name=\"txtNum\" value=\"0\" style=\"width:20px\" />" + 
                     "</td>" +
                         "<td width=\"10%\" align=\"right\">" +
                             "<img src=\""+ getDeleteImage()+ "\" onclick=\"goods_deleteGoods('Bdragtable" + arrValues[0] + "')\" />"+
                         "</td>" +
                         "<td width=\"10%\" class=\"BgoodsLinksright\"></td>"+
                     "</tr>" +
                 "</table>" +
                 "<input type=\"hidden\" id=\"hidGoodsSupplierName" + arrValues[0]+"\" name=\"hidGoodsSupplierName\" value=\"" + arrValues[1] + "\" />"+
                 "<input type=\"hidden\" id=\"hidGoodsSupplierNo" + arrValues[0]+"\" name=\"hidGoodsSupplierNo\" value=\"" + arrValues[0] + "\" />"+
             "</li>";
    return strLi;
}
function goods_supplierSearchClick() {
    goods_supplierSearch(1, "", "", "1");
    setSearchOrder("orderHead");
}
function goods_supplierSearch(intCurPage, strOrderColumn, strOrder, strFlag, pageSize) {
    var arrSupplierNo = new Array();
    var arrObjSupplierNo = document.getElementsByName("hidGoodsSupplierNo");
    for (i = 0;i < arrObjSupplierNo.length ;i++) {
        arrSupplierNo[i] = arrObjSupplierNo[i].value;
    }
    if (strFlag == "1"){
        ajaxRequest(getHost()+"goods/goodsWithSupplier/search", {
            "curPage"       : intCurPage,
            "pageSize"      : pageSize,
            "SupplierId"    : getValue("txtSupplierId"),
            "SupplierName"  : getValue("txtSupplierName"),
            "ProvCode"      : getSelectedValue("selProv"),
            "SupplierNo"    : arrayToJson(arrSupplierNo),
            "Flag"          : strFlag
        }, goods_supplierListView);
    } else {
        ajaxRequest(getHost()+"goods/goodsWithSupplier/search", {
            "curPage"       : intCurPage,
            "pageSize"      : pageSize,
            "orderColumn"   : strOrderColumn,
            "order"         : strOrder,
            "SupplierNo"    : arrayToJson(arrSupplierNo),
            "Flag"          : strFlag
        }, goods_supplierListView);
    }
}
function goods_supplierListView(data) {
    var strLi = "";
    var data_table= "";
    if (data.count > 0) {
        pageCreat(data.curPage, data.count, "pageHead");
        pageCreat(data.curPage, data.count, "pageFoot");
        for(i = 0; data.searchData[i]; i++) {
            strLi = "";
            strLi = strLi + "<li id='goodsList" + data.searchData[i].SUPPLIER_NO + "' >";
            strLi = strLi + "<table width='100%' class='BListTable' >";
            if (data.searchData[i].disableFlag == "1"){
                strLi = strLi + "<tr class = \"BClickmoveGrayoutOdd\" flag=\"1\">";
            }else{
                strLi = strLi + "<tr class = \"BClickmoveOdd\" >";
            }
            strLi = strLi + "<td width=\"10%\" align=\"left\" class=\"title1\">";
            strLi = strLi + "<img id=\"imgDelete\" src=\"" + getHost() + "theme/commonTheme/img/ec-shop_icon1.gif\"/>";
            strLi = strLi + ((data.curPage - 1) * getPageSize() + i +1);
            strLi = strLi + "</td>";
            strLi = strLi + "<td width=\"20%\" align=\"left\" class=\"title1\">";
            strLi = strLi + "<a href=" + getHost()+ "supplier/supplierView/index/SupplierNo/"+ data.searchData[i].SUPPLIER_NO + ">";
            strLi = strLi + data.searchData[i].SUPPLIER_ID + "</a>";
            strLi = strLi + "</td>";
            strLi = strLi + "<td width=\"30%\" align=\"left\" class=\"title1\"><div class=\"Ctxtparagraphleft\" style=\"height:50px; margin-top:5px;\">";
            strLi = strLi + data.searchData[i].SUPPLIER_NAME;
            strLi = strLi + "</div></td>";
            strLi = strLi + "<td width=\"40%\" align=\"left\" class=\"title1\"><div class=\"Ctxtparagraphleft\" style=\"height:50px; margin-top:5px;\">";
            strLi = strLi + data.searchData[i].PROV_CODE;
            strLi = strLi + "</div></td>";
            strLi = strLi + "</tr>";
            strLi = strLi + "</table>";
            strLi = strLi + "<input type=\"hidden\" id=\"hidSupplierNo\" ";
            strLi = strLi + "name=\"hidSupplierNo\" value=\"";
            strLi = strLi + data.searchData[i].SUPPLIER_NO+"\" />";
            strLi = strLi + "<input type=\"hidden\" id=\"hidSupplierName\" ";
            strLi = strLi + "name=\"hidSupplierName\" value=\"";
            strLi = strLi + data.searchData[i].SUPPLIER_NAME+"\" />";
            strLi = strLi + "</li>";
            data_table = data_table + strLi;
        }
        setInnerHTML("goodsList", data_table);
        goods_setTransferTable();
    }else {
        setInnerHTML("goodsList", data_table);
        pageCreat(data.curPage, data.count, "pageHead");
        pageCreat(data.curPage, data.count, "pageFoot");
        var arrErrorMsg = new Array();
        arrErrorMsg[arrErrorMsg.length] = getMessage("ERR-100002");
        clearErrorMsg();
        setErrorMsg(arrErrorMsg,600);
    }
}
function goods_conditionClearClick() {
    setValue("txtSupplierName","");
    setValue("txtSupplierId","");
    setSelectedByValue("selProv","");
}
function goods_GoodsSupplierSaveClick() {
    var arrSupplierName = new Array();
    var arrSupplierNo = new Array();
    var arrObjSupplierName = document.getElementsByName("hidGoodsSupplierName");
    var arrObjSupplierNo = document.getElementsByName("hidGoodsSupplierNo");
    for (i = 0;i < arrObjSupplierName.length ;i++) {
        arrSupplierName[i] = arrObjSupplierName[i].value;
    }
    for (i = 0;i < arrObjSupplierNo.length ;i++) {
        arrSupplierNo[i] = arrObjSupplierNo[i].value;
    }
    ajaxRequest(getHost()+"goods/goodsWithSupplier/save", {
        "SupplierName"      : arrayToJson(arrSupplierName),
        "SupplierNo"        : arrayToJson(arrSupplierNo)
    }, goods_GoodsSupplierSaveCallback);
}
function goods_GoodsSupplierSaveCallback() {
    if(getValue("hidaddEditFg") == 1) {
        document.goodsWithSupplierForm.action = getHost() + "goods/goodsAddEdit/index/returnFg/1";
        document.goodsWithSupplierForm.submit();
    } else {
        document.goodsWithSupplierForm.action = getHost() + "goods/goodsAddEdit/goodsedit/returnFg/1";
        document.goodsWithSupplierForm.submit();
    }
}
function goods_back() {
    if(getValue("hidaddEditFg") == 1) {
        document.goodsWithSupplierForm.action = getHost() + "goods/goodsAddEdit/index/returnFg/1";
        document.goodsWithSupplierForm.submit();
    } else {
        document.goodsWithSupplierForm.action = getHost() + "goods/goodsAddEdit/goodsedit/returnFg/1";
        document.goodsWithSupplierForm.submit();
    }
}
function gotoPage(strPageContainerId, strOrderContainerId) {
    goods_supplierSearch(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function firstPage(strPageContainerId, strOrderContainerId) {
    goods_supplierSearch(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function prePage(strPageContainerId, strOrderContainerId) {
    goods_supplierSearch(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function nextPage(strPageContainerId, strOrderContainerId) {
    goods_supplierSearch(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function lastPage(strPageContainerId, strOrderContainerId) {
    goods_supplierSearch(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}

function orderSearch(strPageContainerId, strOrderContainerId) {
    goods_supplierSearch(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}
function sizeChangeSearch(strPageContainerId, strOrderContainerId) {
    goods_supplierSearch(getCurPage(strPageContainerId), getOrderColumn(strOrderContainerId), getOrder(strOrderContainerId), "2", getPageSize(strPageContainerId));
}